package MOLE.Bill;

import java.util.*;
import MOLE.*;

/** 
 Term ��ժҪ˵����
*/
public class Term
{
	public String BingoNumber = "";
	//����ʱ��
	//��ֹʱ��
	//23:59:59==86399
	public static int StartSecs = 35280 + 3600;
	 public static String EndTime = "22:48:00";
	public static String StartTime = "10:48:00"; //35280 =9:48  :int format
	private int PerTermSpan = 600; // 600sec

	private int WaitTimeWherNexttermStar = 0; //���ڿ�ʼʤ������.
	private String WaitTimeWherNexttermStarInTimeFormat = "00:00"; //���ڿ�ʼʤ������.
	//	public	int StartSeconds;
	public String TermInStrFormat = "19850128-00"; //cyihaor txt format
	public String TermInStrFormatV2 = "19850128-00"; //cyihaor txt format v2
	public int TermInIntFormat = 0;

	private String ThisTermStartTime = "10:00:00";
	private String ThisTermEndTime = "10:00:00";
	 public String KaijeoTime = "00:00:00";






	public Term(String term)
	{
		//
		// TODO: �ڴ˴���ӹ��캯���߼�
		//

		String s = "select * from qishu where qihao='" + term + "'";
		System.Data.DataTable d = MOLE.SQL.GetDataTable(s);
		if (d.Rows.size() != 0)
		{
			String t = d.Rows[0]["kaij"].toString().trim();
			BingoNumber = t;
		}


		TermInStrFormat = term;
		TermInIntFormat = GetCyihaor();
		ThisTermStartTime = _GetThisTermStarttime();
		ThisTermEndTime = _GetThisTermEndtime();
		KaijeoTime = GetBingoTime(TermInStrFormat);

	}

	public Term()
	{


	}


	/** 
	 Get a Now Term
	 
	 @param i
	*/
	public Term(int i)
	{

		WaitTimeWherNexttermStar = NextStartTimeWait();
		TermInIntFormat = Cyihaor();
		TermInStrFormat = MOLE.comm.getDate() + "-" + MOLE.Time.ToDoubleFormat(TermInIntFormat);
		KaijeoTime = GetBingoTime(TermInStrFormat);
	}



	/** 
	 GetThisTermStarttime()
	 
	 @return 
	*/
	public final String _GetThisTermStarttime()
	{
		int startSec = GetStartSec();
		int moreSec = PerTermSpan * (TermInIntFormat - 1);
		int pastSecondesFromStarttime = startSec + moreSec;


		return MOLE.Time.ConvertSecsToTimeformat(pastSecondesFromStarttime);

	}

	/** 
	 
	 
	 @return 
	*/
	public final String _GetThisTermEndtime()
	{

		int startSec = GetStartSec();
		int moreSec = PerTermSpan * TermInIntFormat;
		int pastSecondesFromStarttime = startSec + moreSec;


		return MOLE.Time.ConvertSecsToTimeformat(pastSecondesFromStarttime);

	}

	public final int GetCyihaor()
	{
		int i = Integer.parseInt(TermInStrFormat.substring(9));
		return i;

	}


	// 

	/** 
	 	 Is over Time;
	 
	 @param term
	 @return 
	*/
	public static boolean IsOverTime(String term)
	{
		String nowTermStr = (new Term(1)).TermInStrFormat;
		if (term.equals(nowTermStr)) //current term
		{
			return true;
		}
		else
		{
			term = term.replace("-","");
			nowTermStr = nowTermStr.replace("-","");
			long nowTerm = MOLE.str.ConvertToLong(nowTermStr);
			long inputTerm = MOLE.str.ConvertToLong(term);
			if (inputTerm < nowTerm)
			{
				return true;
			}
			else
			{
				return false;
			}


		}


	}


	public final ArrayList SetBingoNubmer(String BingoNumber)
	{

			ArrayList a = new ArrayList();


			String sql = "update qishu set kaij='" + BingoNumber + "'  where qihao='" + TermInStrFormat + "'";
			Hashtable h = new Hashtable();
			h.put(sql,null);

			a.add(h);

			return a;

	}


//		public static bool  IsOverTime(string term)
//		{








//C# TO JAVA CONVERTER TODO TASK: There is no preprocessor in Java:
		///#region
	public final int GetStartSec()
	{
		String [] a = StartTime.split("[:]", -1);
		int h = Integer.parseInt(a[0]);
		int m = Integer.parseInt(a[1]);
		int s = Integer.parseInt(a[2]);
		int startsecs = h * 3600 + m * 60 + s;
		return startsecs;

	}



	public static int GetNowSeconds()
	{

		int h = java.time.LocalDateTime.now().getHour();
		int m = java.time.LocalDateTime.now().getMinute();
		int s = java.time.LocalDateTime.now().getSecond();
		int startsecs = h * 3600 + m * 60 + s;
		return startsecs;

	}


	public final int NextStartTimeWait()
	{
//C# TO JAVA CONVERTER TODO TASK: There is no preprocessor in Java:
			///#region
		int Allpasttime = GetNowSeconds();
		int start = GetStartSec();
		int span = Allpasttime - start;
		if (span < 0)
		{
			return 0;
		}
		int i = span % PerTermSpan; //past sec

		return PerTermSpan - i;

//C# TO JAVA CONVERTER TODO TASK: There is no preprocessor in Java:
			///#endregion
	}

	public final int NextStartTimeWaitTrue()
	{
//C# TO JAVA CONVERTER TODO TASK: There is no preprocessor in Java:
			///#region
		int Allpasttime = GetNowSeconds();
		int start = GetStartSec();
		int span = Math.abs(Allpasttime - start);
	//	if(span<0)return 0;
		int i = span % PerTermSpan; //past sec
		if (Allpasttime > start)
		{
			return PerTermSpan - i;
		}
		else
		{
			return i;
		}

//C# TO JAVA CONVERTER TODO TASK: There is no preprocessor in Java:
			///#endregion
	}

	public final int Cyihaor()
	{
//C# TO JAVA CONVERTER TODO TASK: There is no preprocessor in Java:
			///#region
		int span = GetNowSeconds() - GetStartSec();
		//	if(span<0)return 0;
		double d = (double)span / (double)PerTermSpan;

		int qihao = (int)Math.ceil(d);


		return qihao;
//C# TO JAVA CONVERTER TODO TASK: There is no preprocessor in Java:
			///#endregion
	}

//C# TO JAVA CONVERTER TODO TASK: There is no preprocessor in Java:
		///#endregion




	public static String GetNextTerm(String currTerm)
	{
		String d = currTerm.substring(0,8);
		String n = currTerm.substring(9);
		int i = Integer.parseInt(n) + 1;
		String r = d + "-" + MOLE.Time.GetDouble(i);
		return r;
	}


	public static String GetBingoTime(String currTerm)
	{
		String d = currTerm.substring(0,8);
		d = MOLE.Time.GetDate(d, "-");

		String n = currTerm.substring(9);
		int i = Integer.parseInt(n);
		int time = i * 600 + StartSecs + 120;
	//	time+=3600  ;//tenno syalinzhi
		String r = d + " " + MOLE.Time.ConvertSecsToTimeformat(time);
		return r;
	}


/** 
 
 
 @param date yyyymmm
 @return 
*/
	public static DataTable GetTermList(String yyyymmdd)
	{

		DataTable t = new DataTable("Aim");

		//Add HEad
		t.Columns.Add(new DataColumn("term"));
		t.Columns.Add(new DataColumn("termmsg"));
		t.Columns.Add(new DataColumn("time"));
		//t.Columns.Add ( new DataColumn("hoho"));


		String time = yyyymmdd;
		for (int i = 1;i <= 72;i++)
		{
			String term = time + "-" + MOLE.Time.GetDouble(i);
			MOLE.Bill.Term oo = new MOLE.Bill.Term(term);
			String kaijtime = oo.KaijeoTime;
		//	str+=term+"      -----------------"+kaijtime+"<p>";

			DataRow r = t.NewRow();

			//------



			//-------
			r.setItem(0, term);
			r.setItem(1, term);
			r.setItem(2, kaijtime);
			//r[2] =  "333";

			t.Rows.Add(r);

		}
		return t;
	}


	public static DataTable GetTermList()
	{
	//	MOLE.comm.getDate();

	DataTable d1 = GetTermList(MOLE.Time.GetDate());

	DataTable d2 = GetTermList(MOLE.Time.AddDate(MOLE.Time.GetDate(), 1));
		//************Add today 
//			for(int j=0;j<d1.Rows.Count;j++)
//			{
//				d1.Rows[j][1]=""+d1.Rows[j][1].ToString();
//
//			}
		for (int j = 0;j < d2.Rows.size();j++)
		{
			d2.Rows[j][1] = "(����)" + d2.Rows[j][1].toString();

		}
		//*************
	DataTable d = comm.HabinDatatable(d1, d2);


	Term o = new Term(0);
	int i = o.TermInIntFormat;


		if (IsStartBetToday())
		{

			for (int j = 0;j < i - 1;j++)
			{
				d.Rows.Remove(d.Rows[0]);

			}
		}
		else if ((GetNowSeconds() >= MOLE.Time.CovertTimeToSecs(EndTime)) && (GetNowSeconds() <= 86399))
		{ //11��00��������00��00
			for (int j = 0;j <= 71;j++)
			{
				d.Rows.Remove(d.Rows[0]);
			}

		}
		else
		{

				//show all day
		}

//test onle to css it's smaill
//			for(int j=0;j<=d.Rows.Count-5;j++)
//			{
//				d.Rows.Remove(   d.Rows[0]   );
//			}


	return d;





	}

	public static DataTable GetTermListToday()
	{

		DataTable d = GetTermList(MOLE.Time.GetDate());


		Term o = new Term(0);
		int i = o.TermInIntFormat;
		if (IsStartBetToday())
		{

			for (int j = 0;j < i - 1;j++)
			{
				d.Rows.Remove(d.Rows[0]);

			}
		}
		else if ((GetNowSeconds() >= MOLE.Time.CovertTimeToSecs(EndTime)) && (GetNowSeconds() <= 86399))
		{ //11��00��������00��00
			for (int j = 0;j <= 71;j++)
			{
				d.Rows.Remove(d.Rows[0]);
			}

		}
		else
		{

			//show all day
		}

		return d;

	}
	public static boolean IsStartBetToday()
	{
		if (GetNowSeconds() >= StartSecs && GetNowSeconds() < MOLE.Time.CovertTimeToSecs(EndTime))
		{
			return true;
		}
		else
		{
			return false;
		}

	}

	public static String GetExTerm()
	{
		String exerm = "";
		int curtermx = (new MOLE.Bill.Term(0)).TermInIntFormat;
		if (curtermx <= 0)
		{
			String date = java.time.LocalDateTime.now().plusDays(-1).toString("yyyyMMdd");
			exerm = date + "-72";
		}
		else if (curtermx > 72)
		{
			String date = java.time.LocalDateTime.now().toString("yyyyMMdd");
			exerm = date + "-72";
		}
		else
		{
			String date = java.time.LocalDateTime.now().toString("yyyyMMdd");
			int exterm = curtermx - 1;
			String extermx = MOLE.comm.AddPreZero(exterm, 2);
			exerm = date + "-" + extermx.toString();
		}

		return exerm;
	}


	//		public  int GetBingoNumber()
	//		{
	//			
	//
	//
	//		}
}